//
//  Course.m
//  Gradekeeper0.2
//
//  Created by Qian Tim on 09-11-12.
//  Copyright 2009 __MyCompanyName__. All rights reserved.
//

#import "Courses.h"

@implementation Courses

-(Courses*) initWithCatagory: (NSString*) newCatagory number: (int) newNumber andCredit: (int) newCredit {
	self = [super init];
	
    if ( self ) {
        workTypes = [[NSMutableArray alloc] initWithCapacity:3];
		weightOfWorks = [[NSMutableArray alloc] initWithCapacity:3];
		CourseWorks = [[NSMutableArray alloc] initWithCapacity:5];
		self.catagory = catagory;
		self.number = number;
		self.credit = credit;
    }
    return self;
}

-(void) setCatagory: (NSString*) newCatagory {
	[catagory release];
	catagory = newCatagory;
}

-(void) setNumber: (int) newNumber {
	number = newNumber;
}

-(void) setCredit: (int) newCredit {
	credit = newCredit;
}

//the datastructure for Courses is alphabetically sorted by insertion sort, and auto-sorts upon item entry.
-(BOOL) addWork: (CourseWork*) work {
	NSEnumerator *enumerator = [CourseWorks objectEnumerator];
    id obj;
	NSUInteger i = 0;
	
    while ( obj = [enumerator nextObject] ) {
        if ([obj getName] > [work getName]) {
			[CourseWorks insertObject:work atIndex:i];
			return YES;
		} else if ([obj getName] == [work getName]) {
			return NO;
		}
		i++;
    }
	
	[CourseWorks addObject:work];
	return YES;
}

-(BOOL) removeWork: (NSString*) name {
	NSEnumerator *enumerator = [CourseWorks objectEnumerator];
    id obj;
	NSUInteger i = 0;
	
	while ( obj = [enumerator nextObject] ) {
        if ([[obj getName] isEqualToString:name]) {
			[CourseWorks removeObjectAtIndex:i];
			return YES;
		}
		i++;
    }			
	return NO;
}

-(CourseWork*) getWork: (NSString*) name {
	NSEnumerator *enumerator = [CourseWorks objectEnumerator];
    id obj;
	
	while ( obj = [enumerator nextObject] ) {
        if ([[obj getName] isEqualToString:name]) {
			return obj;
		}
    }
	return nil;
}

-(NSString*) getCatagory {
	return catagory;
}

-(float) getGrade {
	return grade;
}

-(int) getCredit {
	return credit;
}

-(NSMutableArray*) getWeights {
	return weightOfWorks;
}

-(float) getWeight: (int) index {
	return [[weightOfWorks objectAtIndex:index] floatValue];
}

-(BOOL) setWeights: (NSMutableArray*) weights {
	[weightOfWorks release];
	weightOfWorks = weights;
	return YES;
}

-(BOOL) setWeight: (int) index: (float) value {
	if (index < [weightOfWorks count]) {
		[weightOfWorks replaceObjectAtIndex:index withObject:[NSNumber numberWithFloat:value]];
		return YES;
	} else if (index == [weightOfWorks count]) {
		[weightOfWorks addObject:[NSNumber numberWithFloat:value]];
		return YES;
	}
	return NO;
}

-(void) setGrade: (float) newGrade {
	grade = newGrade;
}
		 
-(void) calcGrade {
	NSMutableArray *workTypedCollections = [NSMutableArray arrayWithCapacity:[workTypes count]];
	NSUInteger i;
	for (i=0; i<[workTypes count]; i++) {
		NSEnumerator *enumerator = [CourseWorks objectEnumerator];
		id obj;
		NSMutableArray *workCollection = [NSMutableArray arrayWithCapacity:1];
		
		while ( obj = [enumerator nextObject] ) {
			if ([obj getType] == [workTypes objectAtIndex:i]) {
				[workCollection addObject:obj];
			}
		}
		[workTypedCollections addObject:workCollection];
	}
	
	float currGrade = 0;
	for (i=0; i<[workTypedCollections count]; i++) {
		float typeAvg = 0;
		NSEnumerator *enumerator = [[workTypedCollections objectAtIndex:i] objectEnumerator];
		id obj;
		
		while (obj = [enumerator nextObject]) {
			typeAvg += [obj getPercentage];
		}
		typeAvg = typeAvg/[[workTypedCollections objectAtIndex:i] count];
		currGrade += typeAvg * [[weightOfWorks objectAtIndex:i] floatValue];
	}
	[self setGrade:currGrade];
}

-(void) dealloc {
    [super dealloc];
	[workTypes dealloc];
	[weightOfWorks dealloc];
	[CourseWorks dealloc];
}


@end
